package com.example.dao.implDao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dao.StuInfoDao;
import com.example.mapper.StuMapper;
import com.example.pojo.StuInfo;
import com.example.pojo.TeacherInfo;
import com.example.pojo.dto.StuInfoDTO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Repository
public class StuInfoDaoImpl implements StuInfoDao {
    @Resource
    private StuMapper stuMapper;

    @Override
    public List<StuInfo> stuInfoList() {
        return stuMapper.selectList(null);
    }

    @Override
    public StuInfoDTO stuInfoDtoByID(Long stuId) {
        MPJLambdaWrapper<StuInfo> wrapper = new MPJLambdaWrapper<StuInfo>()
                .selectAll(StuInfo.class)//查询user表全部字段
                .select(TeacherInfo::getEmail,TeacherInfo::getTeacherName)
                .eq(StuInfo::getId,stuId)
                .leftJoin(TeacherInfo.class, TeacherInfo::getTeacherId, StuInfo::getClassTeacherId);
        return stuMapper.selectJoinOne(StuInfoDTO.class, wrapper);
    }

    @Override
    public String addOrUpdateStuInfo(StuInfo stuInfo) {
        String result = "";
        if(Objects.isNull(stuInfo.getId())) {
            stuMapper.insert(stuInfo);
            result="添加成功";
        }else{
            stuMapper.updateById(stuInfo);
            result="修改成功";
        }
        return result;
    }

    @Override
    public String deleteStuInfo(Long stuId) {
        String result = "删除失败，请稍后再试";
        int i = stuMapper.deleteById(stuId);
        if(i>0){
            result="删除成功";
        }
        return result;
    }

    @Override
    public IPage<StuInfo> stuInfoListPage(int currentPage, int limit) {
        Page<StuInfo> stuInfoPage = new Page<>();
        return stuMapper.selectPage(stuInfoPage,null);
    }

    @Override
    public boolean getStuByBindCode(String bindCode) {
        StuInfo stuInfoByBindCode = stuMapper.getStuInfoByBindCode(bindCode);
        return !Objects.isNull(stuInfoByBindCode);
    }

    @Override
    public StuInfo getStuByName(String stuName) {
        QueryWrapper<StuInfo> stuInfoQueryWrapper = new QueryWrapper<>();
        stuInfoQueryWrapper.eq("name",stuName);
        return stuMapper.selectOne(stuInfoQueryWrapper);
    }
}
